//bench.js 
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({
  data: {
    pileCode: '',
    pileLocationInfo: {},
    changedLocationInfo: {},
    locationArray: ['门诊', '急诊', '住院部'],
    categoryInfoList:[],
    locationModalShow: false,
    locationIndex: 0,
    imgArray: [],
    currentText: ''
  },
  onLoad: function (options) {
    let that = this
    console.log(options)
    this.data.categoryId = options.categoryId;
    let pileLocationInfo = {
      hospitalId: options.hospitalId,
      hospitalName: options.hospitalName,
      location: options.location,
    }
    let changedLocationInfo = {
      hospitalId: options.hospitalId,
      hospitalName: options.hospitalName,
      location: that.data.locationArray[that.data.locationIndex]
    }
    that.setData({
      pileCode: options.pileCode,
      pileType: options.pileType == '担架' ? 2 : 1,
      // pileLocationInfo: pileLocationInfo,
      changedLocationInfo: changedLocationInfo,
      scrollHeight:app.getScrollHeight(0)
    })
    console.log(that.data)
  },
  onReady: function () {
    this.personalModal = this.selectComponent("#personalModal")
  },
  onShow: function () {
    let that = this
    if (that.data.notShow) {
      return
    }
    let changedLocationInfo = {
      hospitalId: that.data.changedLocationInfo.hospitalId,
      hospitalName: that.data.changedLocationInfo.hospitalName,
      location: that.data.locationArray[that.data.locationIndex]
    }
    that.setData({
      changedLocationInfo: changedLocationInfo
    })
    that.getLocationList()
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  setCurrentText: function(e){
    console.log('setCurrentText>>>>>',e)
    this.setData({
      currentText: e.detail.value
    })
  },
  showLocationModal: function () {
    this.setData({
      locationModalShow: true
    })
  },
  hideLocationModal: function(){
    this.setData({
      locationModalShow: false
    })
  },
  stopMaoPao: function(){
    return
  },
  chooseLocation: function (e) {
    this.setData({
      locationIndex: e.currentTarget.dataset.location,
      locationModalShow: false
    })
  },
  addLocation: function (e) {
    console.log(e)
    let that = this
    let choosedlocation = e.detail.value.location
    if (!choosedlocation) {
      wx.showToast({
        title: '请输入内容',
        image: '/images/gantan.png',
        duration: 2500
      })
      setTimeout(function () {
        that.hideLocationModal()
      }, 2500)
      return
    }
    lbbService.addPosByHos({
        hospitalId: parseInt(that.data.changedLocationInfo.hospitalId),
        position: choosedlocation
      },function(res){
        that.hideLocationModal()
        that.getLocationList()
      })
  },
  chooseHospital: function(){
    wx.navigateTo({
      url: '/pages/chooseHospital/chooseHospital',
    })
  },
  addBack: function (imgIdArray) {
    let that = this
    that.setData({
      imgIdArray
    })
    let cId = that.data.categoryInfoList[that.data.categoryIndex].categoryId
    // 这里变更桩位位置
    lbbService.changePilePosition({
        position: that.data.locationArray[that.data.locationIndex],
        equId: parseInt(that.data.pileCode.replace(/[^0-9]/ig,"")),
        lbmid: parseInt(that.data.changedLocationInfo.hospitalId),
        pileType: cId ? cId : that.data.pileType,
        imgs: that.data.imgIdArray,
        reason: that.data.pileChangeReason,
      },function(res){
        wx.showToast({
          title: '变更成功',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      })
  },
  changePileLocation: function(){
    let that = this
    let formData = that.data.formData
    that.setData({
      pileChangeReason: formData.pileChangeReason,
    })
    let imgArray = that.data.imgArray
    util.upLoadImg(app.globalData.header, imgArray, 0, [], that.addBack)
  },
  formSubmit: function(e){
    let that = this
    let formData = e.detail.value
    let pileCode = that.data.pileCode
    let hospitalName = that.data.changedLocationInfo.hospitalName
    let location = that.data.locationArray[that.data.locationIndex]
    that.setData({
      formData: formData,
    })
    let modalText = '确定将桩位Z' + pileCode + '变更到' + hospitalName + location
    that.personalModal.showModal(modalText, that.changePileLocation)
  },
  addImg: function (e) {
    let that = this
    that.setData({
      notShow: true
    })
    wx.chooseImage({
      count: 12-that.data.imgArray.length,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        for (let i = 0; i < res.tempFilePaths.length;i++){
          that.data.imgArray.push(res.tempFilePaths[i])
        }
        that.setData({
          imgArray: that.data.imgArray
        })
        console.log(that.data.imgArray)
      },
      complete:function(){
        that.setData({
          notShow: false
        })
      }
    })
  },
  deleteImg: function(e){
    let that = this
    let deleteIndex = e.currentTarget.dataset.index
    that.data.imgArray.splice(deleteIndex,1)
    that.setData({
      imgArray: that.data.imgArray
    })
  },
  
  bindCategoryPickerChange:function(e) {
    this.setData({
        categoryIndex: e.detail.value
    })
  },

  getLocationList: function () {
    let that = this
    lbbService.getPosByHos({ hospitalId: parseInt(that.data.changedLocationInfo.hospitalId) },function(res){
        let categoryNameArr = [];
        let categoryIndex = 0;
        if(res.categoryList) {
            res.categoryList.forEach((el, i)=> {
                categoryNameArr.push(el.name);
                if(el.categoryId == that.data.categoryId) {
                    categoryIndex = i;
                }
            });
        }
        that.setData({
            locationArray: res.data,
            categoryInfoList: res.categoryList,
            categoryNameArr: categoryNameArr,
            categoryIndex: categoryIndex
        })
        that.setData({
            locationIndex: that.data.locationArray.length - 1
        })
    })
  },
})